package fr.esgi.pages;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import fr.esgi.classes.Groupe;
import fr.esgi.classes.Projet;
import fr.esgi.donnees.Database;

public class ValidationEditerFeuille extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ValidationEditerFeuille() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Projet p = null;
		ArrayList <Groupe> groupes = null;
		String projet=request.getParameter("projet");
		String groupe=request.getParameter("groupe");
		String promotion=request.getParameter("promotion");
		Database d=new Database();
		try {
			p=d.buildProjet(projet);
			groupes=d.afficherEquipe(projet);	

		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Document document = new Document();

		try {
			PdfWriter.getInstance(document, 
					response.getOutputStream());
		

		document.open();
		document.addTitle("Projet: "+ p.getNom());
		Paragraph preface = new Paragraph();
		//addEmptyLine(preface, 1);
		preface.add(new Paragraph("Projet " + p.getNom()+"\n"));
		document.add(preface);
		preface.add(new Paragraph("\n"));
		PdfPTable table = new PdfPTable(4);
		table.addCell("numero du groupe");
		table.addCell("Nom et prenom des Etudiants");
		table.addCell("commmentaires/appreciations");
		table.addCell("notes");
		for (int i = 0; i <groupes.size(); i++) {
			
			for (int j = 0; j <groupes.get(i).getTabEtudiant().size(); j++) {
				table.addCell(String.valueOf(groupes.get(i).getId()));
				table.addCell(groupes.get(i).getTabEtudiant().get(j).getPrenom()+" "+groupes.get(i).getTabEtudiant().get(j).getNom());
				table.addCell(" ");
				table.addCell(" ");
			}
			
		}
		document.add(table);		
		document.close(); 
		
		
	} catch (DocumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
	}
